package com.longdaji.decoration.ui.login.passwordLogin;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.longdaji.decoration.Constants;
import com.longdaji.decoration.DecorationApplication;
import com.longdaji.decoration.R;
import com.longdaji.decoration.base.BaseActivity;
import com.longdaji.decoration.bean.Account;
import com.longdaji.decoration.ui.login.codeLogin.CodeLoginActivity;
import com.longdaji.decoration.ui.login.forgetPassword.ForgetPasswordActivity;
import com.longdaji.decoration.ui.main.MainActivity;
import com.longdaji.decoration.utils.LogUtil;
import com.longdaji.decoration.utils.PreferencesUtil;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class PasswordLoginActivity extends BaseActivity implements PasswordLoginContract.IView {

    private static final String TAG = "PasswordLoginActivity";

    @Inject
    PasswordLoginContract.IPresenter mPresenter;
    @BindView(R.id.tv_tool_title)
    TextView tvTitle;
    @BindView(R.id.et_username_or_telephone)
    EditText etUsernameOrTelephone;
    @BindView(R.id.et_password)
    EditText etPassword;
    @BindView(R.id.iv_hide_password)
    ImageView ivHidePassword;

    private boolean isPasswordHidden = true;

    //interface test
    @BindView(R.id.et_test_number)
    EditText etTestNumber;
    @BindView(R.id.tv_test_result)
    TextView tvTestResult;
    @BindView(R.id.btn_interface_test)
    Button btnInterfaceTest;

    private SharedPreferences sp;
    private SharedPreferences.Editor editor;

    private ProgressDialog pd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_password_login);
        setUnBinder(ButterKnife.bind(this));

        if (mPresenter != null)
            mPresenter.setView(this);
        tvTitle.setText("密码登录");

        initView();

    }

    private void initView() {
        sp = DecorationApplication.getInstance().getSharedPreferences(Constants.TAO_XIN, MODE_PRIVATE);
        editor = sp.edit();

    }

    public static void start(Context context, Intent extras) {
        Intent intent = new Intent(context, PasswordLoginActivity.class);
        if (extras != null) {
            intent.putExtras(extras);
        }
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        context.startActivity(intent);
    }

    @Override
    protected void onStart() {
        super.onStart();

        onParseIntent();

    }

    private void onParseIntent() {
        Intent intent = getIntent();
        if (intent.hasExtra(Constants.AUTO_LOGIN_USERNAME)) {
            String autoUsername = intent.getStringExtra(Constants.AUTO_LOGIN_USERNAME);
            String autoPassword = intent.getStringExtra(Constants.AUTO_LOGIN_PASSWORD);
            LogUtil.d("sptest", "passwordlogin: " + autoUsername + " ---- " + autoPassword);
            mPresenter.onConfirmClick(autoUsername, autoPassword, null);
        } else if (intent.hasExtra(Constants.REGISTER_USERNAME)) {
            String registerUsername = intent.getStringExtra(Constants.REGISTER_USERNAME);
            String registerPassword = intent.getStringExtra(Constants.REGISTER_PASSWORD);
            mPresenter.onConfirmClick(registerUsername, registerPassword, null);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mPresenter != null)
            mPresenter.detachView();
    }

//    @Override
//    protected void onNewIntent(Intent intent) {
//        LogUtil.i(TAG, "onNewIntent");
//        setIntent(intent);
//        if (intent.hasExtra(Constants.AUTO_LOGIN_USERNAME)) {
//            LogUtil.d(TAG, "11111111111");
//            String username = intent.getStringExtra(Constants.AUTO_LOGIN_USERNAME);
//            String password = intent.getStringExtra(Constants.AUTO_LOGIN_PASSWORD);
//            mPresenter.onConfirmClick(username, password, null);
//        }
//    }

    @OnClick({R.id.rl_tool_back, R.id.btn_confirm, R.id.iv_hide_password,
            R.id.tv_code_login, R.id.tv_forget_password, R.id.btn_interface_test})
    void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_tool_back:
                finish();
                break;
            case R.id.btn_confirm:
                showProgressDialog();
                mPresenter.onConfirmClick(etUsernameOrTelephone.getText().toString(), etPassword.getText().toString(), null);
                break;
            case R.id.iv_hide_password:
                mPresenter.onHidePasswordClick(isPasswordHidden);
                break;
            case R.id.tv_code_login:
                startActivity(new Intent(PasswordLoginActivity.this, CodeLoginActivity.class));
                break;
            case R.id.tv_forget_password:
                LogUtil.d("mytest", "onForgetPasswordClick");
//                startActivity(new Intent(PasswordLoginActivity.this, ForgetPasswordActivity.class));
                startActivity(new Intent(PasswordLoginActivity.this, ForgetPasswordActivity.class));
                break;
            case R.id.btn_interface_test:
                mPresenter.doInterfaceTest(Integer.valueOf(etTestNumber.getText().toString().trim()));
                break;
            default:
                break;
        }
    }

    private void showProgressDialog() {
        if (pd == null) {
            pd = new ProgressDialog(this);
            pd.setMessage("登陆中...");
            pd.setCanceledOnTouchOutside(false);
        }
        if (!pd.isShowing()) {
            pd.show();
        }
    }

    private void hideProgressDialog() {
        if (pd != null && pd.isShowing())
            pd.dismiss();
    }


    @Override
    public void showHidePassword(boolean isHidePassword) {
        if (isHidePassword) {
            isPasswordHidden = true;
            ivHidePassword.setImageResource(R.mipmap.hide_password);
            etPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
            etPassword.setSelection(etPassword.length());
        } else {
            isPasswordHidden = false;
            ivHidePassword.setImageResource(R.mipmap.display_password);
            etPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            etPassword.setSelection(etPassword.length());
        }
    }

    //interface test
    @Override
    public void clearTestResult() {
        tvTestResult.setText("");
    }

    @Override
    public void showTestResult(boolean isSuccess) {
        if (isSuccess) {
            tvTestResult.setText("success");
        } else {
            tvTestResult.setText("fail");
        }
    }

    @Override
    public void showAccountError() {
        hideProgressDialog();
        Toast.makeText(this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void goMainByUsername(Account account) {
        hideProgressDialog();
        Account.setInstance(account);
        DecorationApplication.setJustLook(false);
        LogUtil.d("sptest", Account.getInstance().getUsername() + " ---- " + Account.getInstance().getPassword());
        PreferencesUtil.saveUserAccount(etUsernameOrTelephone.getText().toString().trim());
        PreferencesUtil.saveUserPassword(etPassword.getText().toString().trim());
        MainActivity.start(this);
        finish();
    }

    @Override
    public void showNimLoginFail() {
        hideProgressDialog();
        Toast.makeText(this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
    }
}
